<?php
function _sitemanage_key_contanct_add() {
    $language = array('de','ja','es','tr','fr','zh-hans'); //en is default language;
    $key_contact_info=array();
    $key_contact_info[] = array(
        'name' => "Jorge Pérez Bolanos",
        'region' => "ar",
        'title' => "Mobility Consultant",
        'phone' => "(506) 2431-4650",
        'email' => "jbolanos@crownww.com",
        'city' => "San José",
        'country'=>'Costa Rica',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL."/Jorge Bolanos ar.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Jorge Pérez Bolanos",
        'region' => "cl",
        'title' => "Mobility Consultant",
        'phone' => "(506) 2431-4650",
        'email' => "jbolanos@crownww.com",
        'city' => "San José",
        'country'=>'Costa Rica',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL."/Jorge Bolanos cl.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Julia Meidenstein",
        'region' => "gr",
        'title' => "Senior Mobility Consultant",
        'phone' => "(49) 0171 7911780",
        'email' => "jmeidenstein@crownrelo.com",
        'city' => "Schwalbach GO (P&G offices)",
        'country'=>'Germany',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL."/Julia Meidenstein gr.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Julia Meidenstein",
        'region' => "nl",
        'title' => "Senior Mobility Consultant",
        'phone' => "(49) 0171 7911780",
        'email' => "jmeidenstein@crownrelo.com",
        'city' => "Schwalbach GO (P&G offices)",
        'country'=>'Germany',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL."/Julia Meidenstein nl.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Guity Daris",
        'region' => "ma",
        'title' => "Account Manager",
        'phone' => "(33)130 067526",
        'email' => "gdaris@crownrelo.com",
        'city' => "Paris",
        'country'=>'France',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL . "/Guity Daris.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Valeria Hajdu",
        'region' => "hu",
        'title' => "Move Coordinator",
        'phone' => "(36) 204 8660",
        'email' => "vhajdu@crownrelo.com",
        'city' => "Budapest",
        'country'=>'Hungary',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL . "/Valeria Hajdu hu.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Valeria Hajdu",
        'region' => "cz",
        'title' => "Move Coordinator",
        'phone' => "(36) 204 8660",
        'email' => "vhajdu@crownrelo.com",
        'city' => "Budapest",
        'country'=>'Hungary',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL . "/Valeria Hajdu cz.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Valeria Hajdu",
        'region' => "sk",
        'title' => "Move Coordinator",
        'phone' => "(36) 204 8660",
        'email' => "vhajdu@crownrelo.com",
        'city' => "Budapest",
        'country'=>'Hungary',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL . "/Valeria Hajdu sk.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Valeria Hajdu",
        'region' => "hr",
        'title' => "Move Coordinator",
        'phone' => "(36) 204 8660",
        'email' => "vhajdu@crownrelo.com",
        'city' => "Budapest",
        'country'=>'Hungary',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL . "/Valeria Hajdu hr.jpg",
    );
    $key_contact_info[] = array(
        'name' => "Priscila Koch",
        'region' => "pt",
        'title' => "Mobility Consultant",
        'phone' => "(34)918 782 410",
        'email' => "pkoch@crownrelo.com",
        'city' => "Madrid",
        'country'=>'Spain',
        'photo' => KEY_CONTACT_PHOTO_BASE_URL . "/Priscila Koch.jpg",
    );
    $data = array();
    foreach ($key_contact_info as $person) {
        $person_node = new stdClass();
        /* -----------------------node1 content begin----------------------- */
        $person_node->uid = '1';
        $person_node->name = 'admin';
        $person_node->type = 'key_contact';
        $person_node->language = 'en';
        $person_node->title = $person['title'];
        $person_node->status = 1;
        $person_node->promote = 0;
        $person_node->sticky = 0;
        $person_node->created = REQUEST_TIME;
        $person_node->revision = 0;
        $person_node->menu =
                array(
                    'link_title' => '',
                    'mlid' => 0,
                    'plid' => 0,
                    'menu_name' => 'main-menu',
                    'weight' => 0,
                    'options' =>
                    array(
                    ),
                    'module' => 'menu',
                    'expanded' => 0,
                    'hidden' => 0,
                    'has_children' => 0,
                    'customized' => 0,
                    'parent_depth_limit' => 8,
        );
        $person_node->nid = NULL;
        $person_node->vid = NULL;
        $person_node->is_new = TRUE;
        $person_node->changed = '';
        $person_node->additional_settings__active_tab = '';
        $person_node->log = '';
        $person_node->date = '';
        $person_node->submit = 'Save';
        $person_node->path =
                array(
                    'alias' => '',
                    'pid' => NULL,
                    'source' => NULL,
                    'language' => 'en',
        );
        $person_node->op = 'Save';
        $person_node->field_name =
                array(
                    'und' =>
                    array(
                        0 =>
                        array(
                            'value' => $person['name']
                        ),
                    ),
        );
        $person_node->field_phone =
                array(
                    'und' =>
                    array(
                        0 =>
                        array(
                            'value' => $person['phone'],
                        ),
                    ),
        );
        $person_node->field_email =
                array(
                    'und' =>
                    array(
                        0 =>
                        array(
                            'value' => $person['email'],
                        ),
                    ),
        );
        $person_node->field_thumb = array();
        $person_node->field_city =
                array(
                    'und' =>
                    array(
                        0 =>
                        array(
                            'value' => $person['city'],
                        ),
                    ),
        );
        $person_node->field_country =
                array(
                    'und' =>
                    array(
                        0 =>
                        array(
                            'value' => $person['country'],
                        ),
                    ),
        );        
        $person_node->field_region =
                array(
                    'und' =>
                    array(
                        0 =>
                        array(
                            'value' => $person['region'],
                        ),
                    ),
        );
        $person_node->validated = true;

        /* -----------------------node content end----------------------- */
        $data[] = array(
            'file' => $person['photo'],
            'node' => $person_node,
        );
    }
    foreach ($data as $k => $v) {
        $file_info = key_contact_image_info($v['file']);
        $fid = file_managed($file_info);
        $v['node']->field_thumb =
                array(
                    'und' => array(
                        0 => array(
                            'fid' => $fid,
                            'display' => 1,
                            'description' => '',
                            'upload_button' => "Upload",
                            'remove_button' => "Remove",
                            'upload' => '',
                            'width' => $file_info[0],
                            'height' => $file_info[1],
                            'alt' => '',
                            'title' => '',
                        ),
                    ),
        );
        $node = $v['node'];
        node_save($node);
        $nid = $node->nid;
        key_contact_update_fileinfo($fid, $nid);
        $translation_source = new stdClass();

        /* ----------------------------translation source content begin------------------- */
        $translation_source->vid = $node->vid;
        $translation_source->uid = 1;
        $translation_source->title = $node->title;
        $translation_source->log = "";
        $translation_source->status = 1;
        $translation_source->comment = 0;
        $translation_source->promote = 0;
        $translation_source->sticky = 0;
        $translation_source->nid = $node->nid;
        $translation_source->type = "key_contact";
        $translation_source->language = en;
        $translation_source->created = REQUEST_TIME;
        $translation_source->changed = REQUEST_TIME;
        $translation_source->tnid = 0;
        $translation_source->translate = 0;
        $translation_source->revision_timestamp = REQUEST_TIME;
        $translation_source->revision_uid = 1;
        $translation_source->body = Array
                (
        );

        $translation_source->field_name = Array
            (
            "und" => Array
                (
                0 => Array
                    (
                    "value" => $node->filed_name['und'][0]['value'],
                    "format" => "",
                    "safe_value" => $node->filed_name['und'][0]['value'],
                ),
            ),
        );

        $translation_source->field_phone = Array
            (
            "und" => Array
                (
                0 => Array
                    (
                    "value" => $node->field_phone['und'][0]['value'],
                    "format" => "",
                    "safe_value" => $node->field_phone['und'][0]['value'],
                )
            ),
        );

        $translation_source->field_email = Array
            (
            "und" => Array
                (
                0 => Array
                    (
                    "value" => $node->field_email['und'][0]['value'],
                    "format" => "",
                    "safe_value" => $node->field_email['und'][0]['value'],
                ),
            ),
        );

        $translation_source->field_thumb = Array
            (
            "und" => Array
                (
                0 => Array
                    (
                    "fid" => $fid,
                    "alt" => "",
                    "title" => "",
                    "width" => $file_info[0],
                    "height" => $file_info[1],
                    "uid" => "1",
                    "filename" => $file_info['filename'],
                    "uri" => "publick://key-contact/" . $file_info['filename'],
                    "filemime" => $file_info['mime'],
                    "filesize" => $file_info['filesize'],
                    "status" => 1,
                    "timestamp" => REQUEST_TIME,
                    "rdf_mapping" => Array
                    (
                    ),
                ),
            ),
        );

        $translation_source->field_city = Array
            (
            "und" => Array
                (
                0 => Array
                    (
                    "value" => $node->field_city['und'][0]['value'],
                    "format" => "",
                    "safe_value" => $node->field_city['und'][0]['value'],
                ),
            ),
        );

         $translation_source->field_country = Array
            (
            "und" => Array
                (
                0 => Array
                    (
                    "value" => $node->field_country['und'][0]['value'],
                    "format" => "",
                    "safe_value" => $node->field_country['und'][0]['value'],
                ),
            ),
        );    

        $translation_source->field_region = Array
            (
            "und" => Array
                (
                0 => Array
                    (
                    "value" => $node->field_region['und'][0]['value'],
                ),
            ),
        );

        $translation_source->rdf_mapping = Array
            (
            "rdftype" => Array
                (
                0 => "sioc:Item",
                1 => "foaf:Document",
            ),
            "title" => Array
                (
                "predicates" => Array
                    (
                    0 => "dc:title",
                ),
            ),
            "created" => Array
                (
                "predicates" => Array
                    (
                    0 => "dc:date",
                    1 => "dc:created",
                ),
                "datatype" => "xsd:dateTime",
                "callback" => "date_iso8601",
            ),
            "changed" => Array
                (
                "predicates" => Array
                    (
                    0 => "dc:modified",
                ),
                "datatype" => "xsd:dateTime",
                "callback" => "date_iso8601",
            ),
            "body" => Array
                (
                "predicates" => Array
                    (
                    0 => "content:encoded",
                ),
            ),
            "uid" => Array
                (
                "predicates" => Array
                    (
                    0 => "sioc:has_creator",
                ),
                "type" => "rel",
            ),
            "name" => Array
                (
                "predicates" => Array
                    (
                    0 => "foaf:name",
                ),
            ),
            "comment_count" => Array
                (
                "predicates" => Array
                    (
                    0 => "sioc:num_replies",
                ),
                "datatype" => "xsd:integer",
            ),
            "last_activity" => Array
                (
                "predicates" => Array
                    (
                    0 => "sioc:last_activity_date",
                ),
                "datatype" => "xsd:dateTime",
                "callback" => "date_iso8601",
            )
        );

        $translation_source->name = "admin";
        $translation_source->picture = 0;
        $translation_source->data = "b:0";
        /* ----------------------------translation source content end-------------------- */

        foreach ($language as $l) {
            $translate_node = $v['node'];
            $translate_node->nid = NULL;
            $translate_node->vid = NULL;
            $translate_node->is_new = TRUE;
            $translate_node->translation_source = $translation_source;
            $translate_node->language = $l;
            $translate_node->path['language'] = $l;
            node_save($translate_node);
            $node->nid = $translate_node->nid;
            key_contact_update_fileinfo($fid, $translate_node->nid);
        }
    }
}
function key_contact_image_info($uri = '') {
    $info = array();
    if (!empty($uri) && file_exists($uri)) {
        $info = getimagesize($uri);
        $target_path = DRUPAL_ROOT . '/sites/default/files/public/key-contact/';
        create_folders($target_path);
        $info['filename'] = basename($uri);
        $target_file = $target_path . $info['filename'];
        while (file_exists($target_file)) {
            $filename_info = array();
            $filename_info = explode('.', $info['filename']);
            $info['filename'] = $filename_info[0] . '_0.' . $filename_info[1];
            $target_file = $target_path . $info['filename'];
        }
        if (!copy($uri, $target_path . $info['filename'])) {
            exit();
        }
        $info['filesize'] = filesize($uri);
        //  $info['filemime'] = image_type_to_mime_type($uri);
    }
    return $info;
}

function file_managed($info) {
    $data = array(
        'uid' => 1,
        'filename' => $info['filename'],
        'uri' => "public://key-contact/" . $info['filename'],
        'filemime' => $info['mime'],
        'filesize' => $info['filesize'],
        'status' => 1,
        'timestamp' => REQUEST_TIME,
    );

    drupal_write_record('file_managed', $data);

    return $data['fid'];
}

function key_contact_update_fileinfo($fid, $nid) {
    $data = array(
        'fid' => $fid,
        'module' => 'file',
        'type' => 'node',
        'id' => $nid,
        'count' => 1,
    );
    drupal_write_record('file_userage', $data);
}

function create_folders($dir) {
    return is_dir($dir) or (create_folders(dirname($dir)) and mkdir($dir, 0777));
}

function update_bsettings(){
 $msg_cn = <<< EOT
 <br/>Thank you for submitting the Service Initiation Form for Domestic Relocation Services for <font color="red"><employee_last_name>, <employee_first_name></font> to move from <font color="red"><sending_work_location_site></font> to <font color="red"><receiving_work_location_site>.</font> All Relocation Costs will be charged to Cost Center <font color="red"><receiving_cost_centre>.</font> The employee will be contacted by Crown World Mobility within the next <font color="red">48</font> hours.
<br/><br/>
Important:
<br/><br/> 
<div style="padding-left:35px;line-height:16px"><p style="text-indent:-21px;margin-bottom:0;line-height:16px">&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp; If data associated with this Relocation needs to be update or corrected, please <span style="font-style:italic;text-decoration: underline">do not</span> submit another Initiation Form. Please contact Crown World Mobility immediately and they will advise on procedure or next steps. Contact details: <contact_information></p>
<p style="text-indent:-21px;margin-bottom:0;line-height:16px">&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp; <font color="red">Please note that</font> this form does not replace P&G Internal process to <span style="text-decoration: underline;">notify and submit changes related to:</span></p>
&nbsp;&nbsp;&nbsp;&nbsp;o &nbsp;&nbsp; Change in Job Assignment<br/>
&nbsp;&nbsp;&nbsp;&nbsp;o &nbsp;&nbsp; Salary Changes<br/>
&nbsp;&nbsp;&nbsp;&nbsp;o &nbsp;&nbsp; Change of Company or Cost Centre<br/>
<p style="text-indent:-21px;margin-bottom:0;line-height:16px">&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;  If you want to follow up on status of the submission or should you have further questions related this process please contact Crown directly using contact details above. Otherwise please get in touch with your Regional R&ES Contact. </p>
<p style="margin-bottom:0;line-height:16px">EMEA: p&gdomesticRelo.emea@crownrelo.com</p>
<p style="margin-bottom:0;line-height:16px">ASIA: p&gdomesticRelo.asia@crownrelo.com</p>
<p style="margin-bottom:0;line-height:16px">LA: p&gdomesticRelo.la@crownrelo.com</p>
</div>
<br/>
EOT;
  variable_set('BB_ST_INITIATION_RETURN_MSG_CN',$msg_cn);
}